package com.company.test29;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by v on 16-11-28.
 */
public class Test2911 {
    private static ArrayBlockingQueue<Integer> buffer=
            new ArrayBlockingQueue<Integer>(2);

    public static void main(String[] args) {
        ExecutorService executor= Executors.newCachedThreadPool();

        executor.execute(new ProducerTask());
        executor.execute(new ConsumerTask());
        executor.shutdown();
    }

    private static class ProducerTask implements Runnable{
        @Override
        public void run() {
            try {
                int i=1;
                while (true){
                    System.out.println("Producer writes "+i);
                    buffer.put(i++);
                    Thread.sleep((int)(Math.random()*10000));
                }
            }catch (InterruptedException ex){
                ex.printStackTrace();
            }
        }
    }

    private static class ConsumerTask implements Runnable{
        @Override
        public void run() {
            try {
                while (true){
                    System.out.println("\t\t\tConsumer reads"+buffer.take());
                    Thread.sleep((int)(Math.random()*10000));
                }
            }catch (InterruptedException ex){
                ex.printStackTrace();
            }
        }
    }
}
